package com.jwthhealth_pub.old.subscribe.address;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.common.Address.Address;
import com.jwthhealth_pub.old.common.Address.AddressDao;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.subscribe.SubscribeActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 选择区
 * Created by Administrator on 2017/7/18.
 */

public class SubscribeZoneActivity extends BaseActivity {

    @BindView(R.id.title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.city_list)
    RecyclerView cityList;

    private AddressDao mAddressDao = AddressDao.getInstance();
    private String provinceName;
    private String cityName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscribe_city);

        ButterKnife.bind(this);

        Intent intent = getIntent();
        String cid = intent.getStringExtra(Constant.CITYID);
        provinceName = intent.getStringExtra(Constant.PROVINCENAME);
        cityName = intent.getStringExtra(Constant.CITYNAME);
        int type = getIntent().getIntExtra(Constant.CITYTYPE, 0);// 0 直辖市 1 省
        if (type == 0) {
            cid = cid.substring(0, 3);
        } else {
            cid = cid.substring(0, 4);
        }

        List<Address> zones = mAddressDao.getCountries(cid);
        cityList.setLayoutManager(new LinearLayoutManager(this));
        cityList.setAdapter(new ProvinceAdapter(zones));
    }

    @Override
    protected void title() {
        titleLayout.setTitle(getResources().getString(R.string.subscribe_history_address_zone));
        titleLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }


    private class ProvinceAdapter extends RecyclerView.Adapter {
        private List<Address> zones;

        private ProvinceAdapter(List<Address> zones) {
            this.zones = zones;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(View.inflate(SubscribeZoneActivity.this, R.layout.item_address_subscribe, null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewHolder vh = (ViewHolder) holder;
            final int pos = vh.getAdapterPosition();
            vh.name.setText(zones.get(position).getName());
            vh.arrowImage.setVisibility(View.GONE);
            vh.name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    SubscribeCityActivity subscribeCityActivity = new SubscribeCityActivity();
                    subscribeCityActivity.finish();
                    SubscribeProvinceActivity subscribeProvinceActivity = new SubscribeProvinceActivity();
                    subscribeProvinceActivity.finish();
                    SubscribeHistoryAddressActivity subscribeHistoryAddressActivity = new SubscribeHistoryAddressActivity();
                    subscribeHistoryAddressActivity.finish();

                    String zoneName = zones.get(pos).getName();
                    SubscribeActivity newSubscribeActivity = new SubscribeActivity();
                    Intent intent = new Intent(SubscribeZoneActivity.this, newSubscribeActivity.getClass());
                    App.preferenceUtil.putString(Constant.PROVINCENAME, provinceName);
                    App.preferenceUtil.putString(Constant.CITYNAME, cityName);
                    App.preferenceUtil.putString(Constant.ZONENAME, zoneName);
//                    finish();
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return zones.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView name;
            ImageView arrowImage;

            public ViewHolder(View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.text);
                arrowImage = itemView.findViewById(R.id.arrow_img);
            }
        }
    }
}
